// pages/update-user/update-user.js
const http = require('../../utils/http')
const config = require('../../utils/config')
const api = require('../../utils/extendApi');
const crypto = require('../../utils/crypto')
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid:"",
    name:"",
    password:"",
    mobile:"",
    mail:"",
    avatarUrl: defaultAvatarUrl,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let userid = getApp().globalData.userId;
    //设置个人信息
    this.setData({
      userid:userid
    })
    console.log("update-userid",this.data.userid)
    this.getInfo()
  },

   /***
   * 获取个人信息
   */
  getInfo(e){
    var userid=this.data.userid;
    var page = this
    var params = {
      url: "/user",
      method: "GET",
      data: {
        id:userid
      },
      callBack: (res) => {
        page.setData({
          name:res[0].name,
          password:crypto.decrypt(res[0].password),
          mobile:res[0].mobile,
          mail:res[0].mail,
          avatarUrl:res[0].image
        })
      },
      errCallBack(res){
      }
    }
    http.request(params)
  },

  /**
   * 获取头像、输入值
   */
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
      avatarUrl,
    })
    console.log("avatarUrl",this.data.avatarUrl)
  },
  getInputVal(e) {
    console.log(e);
    if (e.currentTarget.dataset.type == 'name') {
      this.setData({
        name: e.detail.value,
      })
      console.log("name",this.data.name)
    } else if (e.currentTarget.dataset.type == 'mobile') {
      this.setData({
        mobile: e.detail.value,
      })
      console.log("mobile",this.data.mobile)
    } else if (e.currentTarget.dataset.type == 'mail') {
      this.setData({
        mail: e.detail.value,
      })
      console.log("mail",this.data.mail)
    }
  },

  /**
   * 更新用户信息
   */
  updateInfo(e){
    var page = this
    var params = {
      url: "/user",
      method: "PUT",
      data: {
        id:this.data.userid,
        name:this.data.name,
        mail:this.data.mail,
        mobile:this.data.mobile,
        image:this.data.avatarUrl
      },
      callBack: (res) => {
        console.log("updateinfo",res)
      },
      errCallBack(res){
      }
    }
    http.request(params)
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})